package com.liugan.magic.screen;

import org.loon.framework.android.game.core.LSystem;
import org.loon.framework.android.game.core.graphics.Screen;
import org.loon.framework.android.game.core.graphics.component.LButton;
import org.loon.framework.android.game.core.graphics.opengl.GLEx;
import org.loon.framework.android.game.core.graphics.opengl.LTexture;
import org.loon.framework.android.game.core.input.LTouch;
import org.loon.framework.android.game.core.timer.LTimerContext;

import android.util.Log;

import com.liugan.magic.utils.*;

import com.liugan.magic.Constants;

import dalvik.system.VMRuntime;


public class StartScreen extends Screen {

	LButton sound;
	private boolean issound;
	public boolean isIssound() {
		return issound;
	}

	private int layertime=0;
	int a_x,a_y,s_x,s_y,sound_x,sound_y;
	
	boolean g=false;
	public synchronized void setG(boolean g) {
		this.g = g;
	}

	@Override
	public void onLoaded() {
		a_x=Constants.ScreenWIDTH/2-ImagesUtils4start.getinstance().armorgames.getWidth()/2;
		a_y=Constants.ScreenHEIGHT/2-ImagesUtils4start.getinstance().armorgames.getHeight()/2;
		s_x=Constants.ScreenWIDTH/2-ImagesUtils4start.getinstance().start.getWidth()/2;
		s_y=Constants.ScreenHEIGHT/2-ImagesUtils4start.getinstance().start.getHeight()/2;
		
		
		
		
	}

	@Override
	public void alter(LTimerContext timer) {
		
//		if (!g){
//			if (layertime<=1000){
				layertime++;
//			}
//		}
		if (layertime%100==0){
			LSystem.gc();
		}
	}

	@Override
	public void draw(GLEx g) {
		
		if (layertime<200){
			g.drawTexture(ImagesUtils4start.getinstance().armorgames,a_x,a_y );
		}else {
			g.drawTexture(ImagesUtils4start.getinstance().start,s_x,s_y );
			//添加按钮
			if (sound==null){
				
				sound=new LButton(ImagesUtils4layer.getinstance().sound_b[0],null,Constants.ScreenWIDTH-50,Constants.ScreenHEIGHT-50){
					@Override
					public void doClick() {
						if (layertime>200){
							if (issound){
								issound=false;
								LTexture[] buttons = new LTexture[1];
								buttons[0]=ImagesUtils4layer.getinstance().sound_b[1];
								this.setImages(buttons);
								LSystem.gc();
							}else{
								issound=true;
								LTexture[] buttons = new LTexture[1];
								buttons[0]=ImagesUtils4layer.getinstance().sound_b[0];
								this.setImages(buttons);
								LSystem.gc();
							}
							
						}
					}
				};
				//默认打开音乐
				issound=true;
				this.add(sound);
			}
		}
		g.drawString(layertime+"", 300, 100);
	}

	@Override
	public void touchDown(LTouch e) {
		if (!g){
			if (layertime>999){
				if (e.getX()>150&&e.getX()<(Constants.ScreenWIDTH-150)&&e.getY()>100&&e.getY()<(Constants.ScreenHEIGHT-100)){
					setG(true);
					this.setScreen(new GameScreen());
				}
			}
		}
	}

	@Override
	public void touchMove(LTouch e) {

	}

	@Override
	public void touchUp(LTouch e) {

	}

}
